//
//  InNewFeatureControllerViewController.m
//  Inner  Voice
//
//  Created by brother on 16/3/22.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InNewFeatureControllerViewController.h"

#import "InMainTabBarViewController.h"

#define WBNewfeatureCount 4

@interface InNewFeatureControllerViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation InNewFeatureControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1 创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    
    scrollView.frame = self.view.bounds;
    
    [self.view addSubview:scrollView];
    
    self.scrollView = scrollView;
    
    //2 添加图片信息
    CGFloat scrollW = scrollView.frame.size.width;
    
    CGFloat scrollH = scrollView.frame.size.height;
    
    for (int i=0; i<WBNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        CGFloat imageViewX = i * scrollW;
        CGFloat imageViewY = 0;
        CGFloat imageViewW = scrollW;
        CGFloat imageViewH = scrollH;
        imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        // 如果是最后一个imageView，就往里面添加其他内容
        if (i == WBNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    
    scrollView.contentSize = CGSizeMake(WBNewfeatureCount*scrollW, 0);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    
    // PageControl 进行数据分页
    
    UIPageControl *pageController = [[UIPageControl alloc] init];
    pageController.numberOfPages = WBNewfeatureCount;
    pageController.backgroundColor = [UIColor redColor];
    pageController.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageController.pageIndicatorTintColor = [UIColor grayColor];
    CGPoint center = CGPointZero;
    center.x = scrollW * 0.5;
    center.y = scrollH - 50;
    //    pageController.userInteractionEnabled = NO;
    
    //    pageController.bounds = CGRectMake(0, 0, 100, 50);
    pageController.bounds = CGRectZero;
    
    pageController.backgroundColor = [UIColor redColor];
    pageController.center = center;
    [self.view addSubview:pageController];
    self.pageControl = pageController;
    // Do any additional setup after loading the view.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double page = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 1.5 + 0.5  => 2
    // 1.1 + 0.5  => 1
    // 1.6 + 0.5  => 2
    self.pageControl.currentPage = (int)(page + 0.5);
    //self.pageControl.currentPage = page;
}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 1.分享给大家（checkbox）
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //shareBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    
    CGRect shareRect = CGRectZero;
    shareRect.size = CGSizeMake(250, 30);
    shareBtn.frame = shareRect;
    
    CGPoint shareBtnCenter = CGPointZero;
    shareBtnCenter.x = imageView.frame.size.width * 0.5;
    shareBtnCenter.y = imageView.frame.size.height * 0.65;
    shareBtn.center = shareBtnCenter;
    
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
    //    shareBtn.backgroundColor = [UIColor redColor];
    //    shareBtn.imageView.backgroundColor = [UIColor blueColor];
    //    shareBtn.titleLabel.backgroundColor = [UIColor yellowColor];
    
    // top left bottom right
    
    // EdgeInsets: 自切
    // contentEdgeInsets:会影响按钮内部的所有内容（里面的imageView和titleLabel）
    //    shareBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 100, 0, 0);
    
    // titleEdgeInsets:只影响按钮内部的titleLabel
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    // imageEdgeInsets:只影响按钮内部的imageView
    //    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 50);
    
    
    
    //    shareBtn.titleEdgeInsets
    //    shareBtn.imageEdgeInsets
    //    shareBtn.contentEdgeInsets
    
    // 2.开始微博
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    CGRect startRect = CGRectZero;
    startRect.size = startBtn.currentBackgroundImage.size;
    startBtn.frame = startRect;
    
    CGPoint startCenter = startBtn.center;
    startCenter.x = shareBtn.center.x;
    startCenter.y = imageView.frame.size.height * 0.75;
    startBtn.center = startCenter;
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)shareClick:(UIButton *)shareBtn
{
    // 状态取反
    shareBtn.selected = !shareBtn.isSelected;
}

- (void)startClick
{
    /*
     }
     切换控制器的手段
     1.push：依赖于UINavigationController，控制器的切换是可逆的，比如A切换到B，B又可以回到A
     2.modal：控制器的切换是可逆的，比如A切换到B，B又可以回到A
     3.切换window的rootViewController
     */
    
    //    [self presentViewController:[[WBTabBarViewController alloc] init] animated:YES completion:nil];
    //
    //    [self.navigationController pushViewController:[[WBTabBarViewController alloc] init] animated:YES];
    
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[InMainTabBarViewController alloc] init];
    
}

- (void)dealloc
{
    
    NSLog(@"新特性 %s",__func__);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
